package com.jswlw.controller.admin.base;

import com.jswlw.controller.BaseController;
import com.jswlw.entity.base.Menu;
import com.jswlw.service.base.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台菜单管理
 * Created by wu on 2017-12-07.
 */
@Controller
@RequestMapping("/admin/menu")
public class AdminMenuController extends BaseController {

    @Resource
    MenuService menuService;

    /**
     * 主页
     *
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model) {
        return "menu/index";
    }

    /**
     * 菜单列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(Model model) {
        return this.successResult(menuService.treeList());
    }

    /**
     * 新增菜单
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        Map condition = new HashMap();
        condition.put("pId", "0");
        modelAndView.addObject("menus", menuService.findAll(condition));
        modelAndView.setViewName("menu/edit");
        return modelAndView;
    }

    /**
     * 编辑菜单
     *
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("info", menuService.findOne(id));
        Map condition = new HashMap();
        condition.put("pId", "0");
        modelAndView.addObject("menus", menuService.findAll(condition));
        modelAndView.setViewName("menu/edit");
        return modelAndView;
    }

    /**
     * 保存菜单
     *
     * @param model
     * @param menu
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(Model model, Menu menu) {
        try {
            if (menu.getId() == null) {
                menuService.insert(menu);
            } else {
                menuService.update(menu);
            }
            return this.successResult(null);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }

    /**
     * 删除菜单
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ModelMap delete(Model model, Integer id) {
        try {
            menuService.delete(id);
            return this.successResult("删除成功");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("删除失败" + e.getMessage());
        }
    }
}
